package com.dynamixsoftware.printershare;

import java.util.Date;

import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityCalendar extends ActivityRoot {

	static final int DIALOG_END = 1;
	static final int DIALOG_RANGE = 2;
	static final int DIALOG_START = 0;
	static final int RANGE_30DAYS = 3;
	static final int RANGE_7DAYS = 2;
	static final int RANGE_CUSTOM = 4;
	static final int RANGE_TODAY = 0;
	static final int RANGE_TOMORROW = 1;
	Time end;
	private OnDateSetListener endDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker var1, int var2, int var3, int var4) {
			ActivityCalendar.this.end.year = var2;
			ActivityCalendar.this.end.month = var3;
			ActivityCalendar.this.end.monthDay = var4;
			ActivityCalendar.this.displayDates();
		}
	};
	private OnItemClickListener listListener = new OnItemClickListener() {
		public void onItemClick(AdapterView var1, View var2, int var3, long var4) {
			if (var3 == 0) {
				ActivityCalendar.this.showDialog(2);
			}

			if (var3 == 1) {
				ActivityCalendar.this.showDialog(0);
			}

			if (var3 == 2) {
				ActivityCalendar.this.showDialog(1);
			}

		}
	};
	ListView mList;
	android.view.View.OnClickListener printClickListener = new android.view.View.OnClickListener() {
		public void onClick(View var1) {
			try {
				if (ActivityCalendar.this.end.before(ActivityCalendar.this.start)) {
					Toast.makeText(ActivityCalendar.this, 2131099689, 1).show();
				} else {
					Intent var3 = new Intent();
					var3.setClass(ActivityCalendar.this, ActivityPrintCalendar.class);
					var3.putExtra("start", ActivityCalendar.this.start.toMillis(false));
					var3.putExtra("end", ActivityCalendar.this.end.toMillis(false));
					ActivityCalendar.this.startActivityForResult(var3, 10);
				}
			} catch (Exception var4) {
				var4.printStackTrace();
				App.reportThrowable(var4);
			}
		}
	};
	int range = 2;
	Time start;
	private OnDateSetListener startDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker var1, int var2, int var3, int var4) {
			ActivityCalendar.this.start.year = var2;
			ActivityCalendar.this.start.month = var3;
			ActivityCalendar.this.start.monthDay = var4;
			ActivityCalendar.this.displayDates();
		}
	};
	Time today_end;
	Time today_start;

	private Dialog createRangeDialog() {
		Builder var1 = new Builder(this);
		var1.setTitle(2131099822);
		var1.setSingleChoiceItems(2130968576, this.range, new OnClickListener() {
			public void onClick(DialogInterface var1, int var2) {
				var1.dismiss();
				ActivityCalendar.this.range = var2;
				ActivityCalendar.this.calculateDates();
				ActivityCalendar.this.displayDates();
			}
		});
		return var1.create();
	}

	private void populateToday() {
		this.today_start = new Time();
		this.today_start.set((new Date()).getTime());
		this.today_start.minute = 0;
		this.today_start.hour = 0;
		this.today_start.second = 0;
		this.today_end = new Time(this.today_start);
		this.today_end.hour = 23;
		this.today_end.minute = 59;
		this.today_end.second = 59;
	}

	void calculateDates() {
		switch (this.range) {
		case 0:
			this.start = new Time(this.today_start);
			this.end = new Time(this.today_end);
			return;
		case 1:
			this.start = new Time(this.today_start);
			Time var7 = this.start;
			++var7.monthDay;
			this.start.normalize(false);
			this.end = new Time(this.today_end);
			Time var10 = this.end;
			++var10.monthDay;
			this.end.normalize(false);
			return;
		case 2:
			this.start = new Time(this.today_start);
			this.end = new Time(this.today_end);
			Time var4 = this.end;
			var4.monthDay += 6;
			this.end.normalize(false);
			return;
		case 3:
			this.start = new Time(this.today_start);
			this.end = new Time(this.today_end);
			Time var1 = this.end;
			var1.monthDay += 29;
			this.end.normalize(false);
			return;
		default:
		}
	}

	protected void displayDates() {
		((BaseAdapter) this.mList.getAdapter()).notifyDataSetChanged();
	}

	public void onCreate(Bundle var1) {
		super.onCreate(var1);
		this.setContentView(2130903041);
		this.setTitle(2131099696);
		this.populateToday();
		this.calculateDates();
		this.mList = (ListView) this.findViewById(2131230725);
		this.mList.setAdapter(new ActivityCalendar.CalendarAdapter());
		this.mList.setOnItemClickListener(this.listListener);
		((Button) this.findViewById(2131230726)).setOnClickListener(this.printClickListener);
	}

	protected Dialog onCreateDialog(int var1) {
		switch (var1) {
		case 0:
			return new DatePickerDialog(this, this.startDateSetListener, this.start.year, this.start.month,
					this.start.monthDay);
		case 1:
			return new DatePickerDialog(this, this.endDateSetListener, this.end.year, this.end.month, this.end.monthDay);
		case 2:
			return this.createRangeDialog();
		default:
			return null;
		}
	}

	protected void onPrepareDialog(int var1, Dialog var2) {
		switch (var1) {
		case 0:
			((DatePickerDialog) var2).updateDate(this.start.year, this.start.month, this.start.monthDay);
			break;
		case 1:
			((DatePickerDialog) var2).updateDate(this.end.year, this.end.month, this.end.monthDay);
		}

		super.onPrepareDialog(var1, var2);
	}

	class CalendarAdapter extends BaseAdapter {

		static final String dateFormat = "%x";

		public int getCount() {
			return 3;
		}

		public Object getItem(int var1) {
			return Integer.valueOf(var1);
		}

		public long getItemId(int var1) {
			return (long) var1;
		}

		public View getView(int var1, View var2, ViewGroup var3) {
			byte var4 = 1;
			if (var2 == null) {
				var2 = ActivityCalendar.this.getLayoutInflater().inflate(2130903042, var3, false);
			}

			TextView var5 = (TextView) var2.findViewById(2131230727);
			TextView var6 = (TextView) var2.findViewById(2131230728);
			ImageView var7 = (ImageView) var2.findViewById(2131230729);
			if (var1 == 0) {
				var5.setText(2131099822);
				var6.setText(ActivityCalendar.this.getResources().getTextArray(2130968576)[ActivityCalendar.this.range]);
				var5.setEnabled((boolean) (var4 == 1));
				var6.setEnabled((boolean) (var4 == 1));
				var7.setVisibility(0);
			}

			if (var1 == var4) {
				var5.setText(2131099823);
				var6.setText(ActivityCalendar.this.start.format("%x"));
				byte var9;
				if (ActivityCalendar.this.range == 4) {
					var9 = var4;
				} else {
					var9 = 0;
				}

				var5.setEnabled((boolean) (var9 == 0));
				byte var10;
				if (ActivityCalendar.this.range == 4) {
					var10 = var4;
				} else {
					var10 = 0;
				}

				var6.setEnabled((boolean) (var10 == 0));
				var7.setVisibility(8);
			}

			if (var1 == 2) {
				var5.setText(2131099824);
				var6.setText(ActivityCalendar.this.end.format("%x"));
				byte var8;
				if (ActivityCalendar.this.range == 4) {
					var8 = var4;
				} else {
					var8 = 0;
				}

				var5.setEnabled((boolean) (var8 == 0));
				if (ActivityCalendar.this.range != 4) {
					var4 = 0;
				}

				var6.setEnabled((boolean) (var4 == 0));
				var7.setVisibility(8);
			}

			return var2;
		}

		public boolean isEnabled(int var1) {
			return var1 != 1 && var1 != 2 || ActivityCalendar.this.range == 4;
		}
	}
}
